package com.meteor.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.meteor.pojo.Book;
import com.meteor.pojo.Shop;
import com.meteor.pojo.User;
import com.meteor.service.BookService;
import com.meteor.service.ShopService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class TypeController {

    @Resource
    private BookService bookService;

    @Resource
    private ShopService shopService;

    // 分类展示
    @GetMapping("/typeId")
    public String selectBookByTypePage(Integer typeId, Model model) {
        PageHelper.startPage(1,1000);
        List<Book> books = bookService.selectBookByTypeId(typeId);
        PageInfo<Book> info = new PageInfo<>(books);
        model.addAttribute("info", info);
        model.addAttribute("books", books);
        return "front/book::tab";
    }

    // 加入购物车
    // 根据传来的bookId查找购物车中是否存在，若存在则count+1，若不存在则插入
    @GetMapping("/addShop")
    public void addShop(Integer bookId, HttpSession session) {
        User user =(User) session.getAttribute("user");
        Shop shop = shopService.selectBookByBookId(bookId);
        if(shop != null) {
            Integer count = shop.getCount();
            shop.setCount(count+1);
            shopService.updateCountByBookId(shop);
        }else {
            Shop shop1 = new Shop();
            shop1.setCount(1);
            shop1.setUserId(user.getId());
            shop1.setBookId(bookId);
            int i = shopService.insertBook(shop1);
        }
    }
}











